package com.wavecoler.professor.bots;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.logging.Logger;


import com.google.wave.api.*;
import com.google.wave.api.event.FormButtonClickedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
import com.wavecoler.coach.objects.Graph;
import com.wavecoler.coach.objects.Node;

import com.wavecoler.professor.dao.ProblemDAO;
import com.wavecoler.professor.objects.Problem;
import com.wavecoler.professor.objects.Word;

public class BotERProblems extends AbstractRobot {

	private static final Logger log = Logger.getLogger(BotERProblems.class.getName());

	private static Gadget gadget;
	private static FormElement glossaryElement;
	private static FormElement problemElement;
	private static FormElement problemDescElement;
	private Problem problem;
	private List<Word> glossary;
	private List<Graph> graphs;
	
	@Override
	protected String getRobotName() {
		// TODO Auto-generated method stub
		return "Bot ER PRoblems";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		// TODO Auto-generated method stub
		return "robotproblemer.appspot.com";
	}

	@Override
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
		Wavelet wave = event.getWavelet();
		wave.setTitle("");

		Blip blip = wave.getBlip(wave.reply("\n").getBlipId());
		blip.append("\n");
		blip.append("\n");
		glossaryElement = new FormElement(ElementType.TEXTAREA, "glossary");
		problemElement = new FormElement(ElementType.INPUT, "problemName");
		problemDescElement = new FormElement(ElementType.TEXTAREA,"problemDescription");
		
		blip.append(new FormElement(ElementType.LABEL, "label_name","Problem Name:"));
		blip.append(problemElement);
		
		blip.append("\n");
		blip.append(new FormElement(ElementType.LABEL, "label_description","Problem Description:"));
		blip.append(problemDescElement);
		blip.append("\n");
		
		blip.append(new FormElement(ElementType.LABEL, "label_glossary","Glossary of Words:"));
		blip.append(glossaryElement);
		
		blip.append("\n");
		blip.append(new FormElement(ElementType.BUTTON, "saveProblem","Save Problem"));
		

	}

	@Override
	public void onFormButtonClicked(FormButtonClickedEvent event) {
		SortedMap<Integer, Element> list = event.getBlip().getElements();
		
		log.info("Click en el boton!!!!!!");
		if (event.getButtonName().equals("saveProblem")) {
			String problemName = "";
			String problemDescription = "";
			String glossary = "";
			String graphs = "";
			
			for (Integer key : list.keySet()) {
				Element elem = list.get(key);
				if (elem.isFormElement()) {
					if (elem.asElement().getProperty("name").equals("problemName")) {
						
						//event.getWavelet().reply("\n "+ key+ "_"+ ((FormElement) elem.asElement()).getValue() + " : "+ elem.asElement().getProperty("name"));
						problemName = ((FormElement) elem.asElement()).getValue();
						//event.getWavelet().reply("\n"+problemName);
						
					} else if (elem.asElement().getProperty("name").equals("problemDescription")) {
						
						//event.getWavelet().reply("\n " + key+ elem.asElement().getProperty("value")+ " : "+ elem.asElement().getProperty("name"));
						problemDescription = ((FormElement) elem.asElement()).getValue();
						//event.getWavelet().reply("\n"+problemDescription);
						
					} else if (elem.asElement().getProperty("name").equals("glossary")) {
						
						//event.getWavelet().reply("\n " + key+ elem.asElement().getProperty("value")+ " : "+ elem.asElement().getProperty("name"));
						glossary = ((FormElement) elem.asElement()).getValue();
						//event.getWavelet().reply("\n"+glossary);
					}

				}
			}
			event.getWavelet().reply("\n guardando......");
			event.getWavelet().reply("\n"+saveProblem(problemName,problemDescription,glossary,graphs,event));

		}
		if (event.getButtonName().equals("saveWord")) {
			event.getWavelet()
					.getBlip(event.getBlip().getBlipId())
					.append("\n ---"
							+ event.getBlip().getElements().get(7)
									.getProperty("value"));

		}

	}
	
	public String saveProblem(String problemName, String problemDescription, String glossary, String graphs, FormButtonClickedEvent event ){
		problem = new Problem();
		this.glossary = new ArrayList<Word>();
		this.graphs = new ArrayList<Graph>();
		problem.setName(problemName);
		problem.setDescription(problemDescription);
		
		this.glossary.add(new Word("algo","algo","algo","algo"));
		
		String[] rows = glossary.trim().split("\n");
		event.getWavelet().reply("\n# de Palabras: "+rows.length);
		for(String row:rows){
			//event.getWavelet().reply("\n"+row);
			String[] words = row.trim().split(",");
			try{
				this.glossary.add(new Word(words[0],words[1],words[2],words[3]));
			}catch(NullPointerException npe){
				event.getWavelet().reply("\n Revise el glosario, existe un error");
			}
			//for(String word: words){
				//event.getWavelet().reply("\n"+word);	
			//}
			//event.getWavelet().reply("\n");
		}
		
		Graph graph = new Graph();
		Node node = new Node("El nodito","nodito",graph); 
		node.setGraph(graph); node.setLabel("El nodo");
		node.setType("Nodo");
		
		List<Node> nodes = new ArrayList<Node>(); nodes.add(node);
		
		
		graph.setExpert(true); graph.setGraphName("El grafito");
		graph.setNodes(nodes);
		
		graph.setGraphName("El grafo"); graph.setProblem(problem);
		graph.setNodes(nodes);
		
		this.graphs.add(graph);
		
//		problem.setGraphs(this.graphs);
//		//problem.setGlossary(this.glossary);
//		
//		//String result = ProblemDAO.insertProblem(problem);
//		//return result;
		return "";
		
	}

}